<?php
namespace app\home\controller;
use cmf\controller\UserBaseController;
use think\Db;

class StreamController extends UserBaseController
{
    //查看流水应该是分权限的，暂时没有加
    function _initialize()
    {
        parent::_initialize();
    }
    public function index()
    {       
       /**搜索条件**/
        $uinfo=cmf_get_current_user();
       $param = $this->request->param();
       $where=array();
       $starttime=isset($param['start_time'])?$param['start_time']:'';
       $endtime=isset($param['end_time'])?$param['end_time']:'';
       $orderNo=isset($param['orderNo'])?$param['orderNo']:'';
       $cardid=isset($param['cardid'])?$param['cardid']:'';
       if(!empty($starttime)){
        	$where['ctime'][]=array('egt',strtotime($starttime));
       }
       if(!empty($endtime)){
        	$where['ctime'][]=array('lt',24*60*60+strtotime($endtime));
       }
       if(!empty($uinfo['storeid'])){
        	$where['storeid']=$uinfo['storeid'];
       }
       if(!empty($orderNo)){
           $where['orderNo']=$orderNo;
       }
       if(!empty($cardid)){
           $where['cardid']=$cardid;
       }
      if(!empty($uinfo['id'])){
        	$where['operatorid']=$uinfo['id'];
       }
       //判断登陆者权限
       $uid=session('ADMIN_ID');
       $uinfo=Db::name('User')->where(array('id'=>$uid))->find();
       if($uinfo['user_type'] == 3){
           $store_list=Db::name('Store')->where(array('steer'=>$uid))->field('id')->select()->toarray();
           foreach($store_list as $k=>$v)
           {
                $new_list[] = $v['id'];
           }
           $string=implode(',',$new_list);
           $where['storeid']=array('in',$string);
       }
       $list=Db::name('Order')->where($where)->order("id DESC")->paginate(10);
       // $list->appends(['g_goodsname' => $goodsname,'g_code' => $code]);
       //echo Db::name('Order')->getlastsql();
       //$list_store_list=Db::name('Store')->where(array('status'=>0))->select()->toarray();//正在运营的店铺列表
       // 获取分页显示
       $page = $list->render();
       $this->assign("uinfo",$uinfo);
       $this->assign("page", $page);
       $this->assign("list", $list);
       return $this->fetch();
    }
    public function detail(){
        $param = $this->request->param();
        $id=$param['id'];
        $goodsname=isset($param['goodsname'])?$param['goodsname']:'';
        if ($goodsname){
            $where['g_goodsname']=['like',"%$goodsname%"];
            $g_list=Db::name('Goods')->where($where)->field('g_id')->select()->toarray();
            $g_id_arr=array();
            foreach ($g_list as $k=>$v){
                $g_id_arr[]=$v['g_id'];
            }
            $g_id_str=implode(',',$g_id_arr);
            $map['goodsid']=array('in',$g_id_str);
        }
        $orderNo=get_order_data($id,'orderNo');
        $map['orderid']=$orderNo;
        $detail_list=Db::name('OrderDetail')->where($map)->paginate(10);
        $page = $detail_list->render();
        $this->assign("detail_list", $detail_list);
        $this->assign("id", $id);
        $this->assign("page", $page);
        return $this->fetch();
    }
    public function statis()
    {  
        $uinfo=cmf_get_current_user();
        $param = $this->request->param();
        $starttime=isset($param['start_time'])?$param['start_time']:'';
        $endtime=isset($param['end_time'])?$param['end_time']:'';
        $where=array();
        if(!empty($starttime)){
            $where['ctime'][]=array('egt',strtotime($starttime));
        }
        if(!empty($endtime)){
            $where['ctime'][]=array('lt',24*60*60+strtotime($endtime));
        }
       if(!empty($uinfo['id'])){
        	$where['operatorid']=$uinfo['id'];
        }
        //$operator=isset($param['operator'])?$param['operator']:'';
        //$operatorid_list=operator_list($operator);
        //if(!empty($operatorid_list)){
        //    $where['operatorid']=array('in',$operatorid_list);
       // }
       $list=Db::name('OperatorStatis')->where($where)->order("id DESC")->paginate(10); 
       // 获取分页显示
       $page = $list->render();
       $this->assign("uinfo", $uinfo);
       $this->assign("page", $page);
       $this->assign("list", $list);
       return $this->fetch();        
    }
    public function supply()
    {
        $uinfo=cmf_get_current_user();
        $param = $this->request->param();
        $id=$param['id'];
        $this->assign("id", $id);
        $this->assign("uinfo", $uinfo);
        return $this->fetch();
    }    
    public function supplyPost(){
        $param = $this->request->param();
        $data=array(
            'pid'=>$param['pid'],
            'storeid'=>$param['storeid'],
            'operatorid'=>$param['operatorid'],
            'money'=>$param['money'],
            'remark'=>$param['remark'],
            'ctime'=>time(),
        );      
        //$actualMoney=Db::name('OperatorStatis')->where(array('id'=>$param['pid']))->field('actualMoney')->find();       
        //$money=$actualMoney['actualMoney']+$param['money'];
        //$re=Db::name('OperatorStatis')->where(array('id'=>$param['pid']))->setField('actualMoney',$money);       
        $re=Db::name('Supply')->insert($data);
        if($re){
           $this->success("修订成功！");
        }else{
           $this->error("修订失败！");
        }
    }
}